package com.lsz.flight_system.service.Impl;

import com.lsz.flight_system.entity.BuyTicketsMessage;
import com.lsz.flight_system.entity.Notice;
import com.lsz.flight_system.repository.BuyTicketsMessageRepository;
import com.lsz.flight_system.repository.NoticeRepository;
import com.lsz.flight_system.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    NoticeRepository noticeRepository;

    @Autowired
    BuyTicketsMessageRepository buyTicketsMessageRepository;

//    @Autowired
//    StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean saveNotice(Notice notice) {
        notice.setSendTime(LocalDateTime.now());
        try {
            noticeRepository.save(notice);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Notice> findAllByNotice(Map<String, Object> map) {
        return noticeRepository.findAll(new Specification<Notice>() {
            @Override
            public Predicate toPredicate(Root<Notice> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                if (map.containsKey("belong") && !StringUtils.isEmpty(map.get("belong"))){
                    predicateList
                            .add(cb.equal(root.get("belong").as(String.class),map.get("belong")));
                }
                if (map.containsKey("sender") && !StringUtils.isEmpty(map.get("sender"))){
                    predicateList
                            .add(cb.like(root.get("sender"),"%"+map.get("sender").toString()+"%"));
                }
                Predicate[] predicateArr = new Predicate[predicateList.size()];
                return cb.and(predicateList.toArray(predicateArr));
            }
        });
    }

    @Override
    public boolean adminSaveNoticeByPlaneId(String planeId, String noticeText) {
        List<BuyTicketsMessage> list = buyTicketsMessageRepository.findBuyTicketsMessagesByPlaneId(planeId);
        //如果一个用户买了两张同一航班号的票，则会发送两个通知
        for (BuyTicketsMessage ticketsMessage:list){
            Notice notice = new Notice();//在for外声明时，自增的ID在添加时只会有一个，发送多个通知时会覆盖之前通知
            notice.setNoticeText(noticeText);
            notice.setSender("管理员");
            notice.setSendTime(LocalDateTime.now());
            notice.setBelong(ticketsMessage.getUsername());
//            notice.setId(notice.getId()+1);
            noticeRepository.save(notice);
        }
        return true;
    }

    @Override
    public boolean adminDelNoticeById(Long id) {
        Notice notice = noticeRepository.findNoticeById(id);
        Notice sendNotice = new Notice();
        sendNotice.setNoticeText("您发送的“"+notice.getNoticeText()+"“通知已被处理");
        sendNotice.setSendTime(LocalDateTime.now());
        sendNotice.setSender("管理员");
        sendNotice.setBelong(notice.getSender());
        sendNotice.setId(notice.getId());
        log.info("sendNotice:"+sendNotice.toString());
//        stringRedisTemplate.opsForValue().set(String.valueOf(id),notice.toString());
        //管理员处理通知后删除
//        noticeRepository.deleteById(id);
        //管理员处理通知后给用户发送处理通知信息
        noticeRepository.save(sendNotice);
        return true;
    }

    @Override
    public boolean delNoticeById(Long id) {
        noticeRepository.deleteById(id);
        return noticeRepository.findNoticeById(id) == null;
    }

    @Override
    public List<Notice> findNoticesByBelong(String belong) {
        return noticeRepository.findNoticesByBelong(belong);
    }
}
